import { camelize, classify, dasherize } from '@angular-devkit/core/src/utils/strings';
import { apply, applyTemplates, mergeWith, move, Rule, SchematicContext, Tree, url } from '@angular-devkit/schematics';
import { normalize } from '@angular-devkit/core';
import * as path from 'path';

// You don't have to export the function as default. You can also have more than one rule factory
// per file.
export function schematics(options: any): Rule {
  return (_tree: Tree, _context: SchematicContext) => {
    // 横线分割:test-user-order
    let originName;
    let outputPath = '';
    if (options.name.indexOf('/') !== -1) {
      originName = options.name.split('/').pop();
      outputPath = options.name.split('/').slice(0, -1).join('/');
    } else {
      originName = options.name;
    }
    let name0 = dasherize(originName);
    // 大驼峰命名:TestUserOrder
    let name1 = classify(camelize(originName));
    outputPath = path.join(options.path, outputPath);
    return mergeWith(apply(url('./files'), [
      applyTemplates({
        name0, name1,
      }),
      move(normalize(outputPath)),
    ]));
  };
}
